home *** CD-ROM | disk | FTP | other *** search
/ Magnum One / Magnum One (Mid-American Digital) (Disc Manufacturing).iso / d12 / c_toolbx.arc / BLOCKIO.C < prev    next >
Encoding:
C/C++ Source or Header  |  1988-03-30  |  2.4 KB  |  94 lines

  1. /*  blockio.c - retrieve/update index blocks for BTREE */
  2. #include   "stdio.h"
  3. #include   "btree.h"
  4. #include   "bt_macro.h"
  5.  
  6. extern     IX_DESC    *pci  ;    /* refers to index descriptor */
  7.                 /* for current function call  */
  8.  
  9. extern     BLOCK     spare_block ;    /* scratch block for splits and */
  10.                 /* compressing */
  11.  
  12. RECPOS    get_free() ;
  13.  
  14. int  init_bio()         /* initialize blockio */
  15.   {
  16.      init_cache() ;        /* init. cache buffers */
  17.   }
  18.  
  19.  
  20. int  retrieve_block(l,r,pb,current)
  21.                 /* retrieve an index block */
  22.   int    l ;            /* index level of this block */
  23.   RECPOS   r ;            /* block's location in index file */
  24.   BLOCK *pb ;            /* put it here */
  25.   int    current ;        /* =1, put in cache */
  26.   {
  27.      if( chk_cache(l,r) != 0 )    /* look in cache first */
  28.     get_cache(l,r,pb) ;    /* there - get it */
  29.      else
  30.     {  read_block(r,pb) ;    /* not there - read it */
  31.        pb->brec = r ;
  32.        pb->lvl  = l ;
  33.        if( current )    /* current block for level l ? */
  34.           put_cache(l,pb) ; /* yes - put in the cache */
  35.     }
  36.      return( IX_OK ) ;
  37.   }
  38.  
  39.  
  40. int  update_block(pb)        /* update an index block in file */
  41.   BLOCK *pb ;            /* address of index block */
  42.   {
  43.      if( chk_cache(pb->lvl,pb->brec) != 0 )
  44.     put_cache(pb->lvl,pb) ;
  45.      write_block(pb->brec,pb) ;
  46.   }
  47.  
  48. int  get_block(l,pb)        /* allocate and set up a block */
  49.                 /* retrieve an index block */
  50.   int    l ;            /* index level for this block */
  51.   BLOCK *pb ;            /* put it here */
  52.   {
  53.      RECPOS   r ;
  54.  
  55.      r = get_free() ;        /* allocate disk block */
  56.      if( r == NULLREC )
  57.     return( IX_FAIL ) ;
  58.      pb->brec = r ;        /* record block's file position */
  59.      pb->lvl  = l ;        /* and it's level */
  60.      return( IX_OK ) ;
  61.   }
  62.  
  63. int  put_free(pb)        /* return block to the free list */
  64.   BLOCK *pb ;
  65.   {
  66.      scrub_cache(pb->brec) ;    /* remove from cache */
  67.      pb->lvl = FREE_LEVEL ;    /*mark as free */
  68.      write_free(pb->brec,pb) ;
  69.   }
  70.  
  71. int  read_block(r,pb)        /* read an index blocl */
  72.   RECPOS   r ;            /* block's location in index file */
  73.   BLOCK *pb ;            /* store the block here */
  74.   {
  75.      int   ret     ;
  76.  
  77.      ret = read_if(r,pb, sizeof(BLOCK) ) ;
  78.      return( ret ) ;
  79.   }
  80.  
  81.  
  82. int  write_block(r,pb)        /* write an index block */
  83.   RECPOS   r ;            /* block's location in index file */
  84.   BLOCK *pb ;            /* the data to write */
  85.   {
  86.      int   ret ;
  87.  
  88.      ret = write_if(r,pb, sizeof(BLOCK) ) ;
  89.      return( ret ) ;
  90.   }
  91.  
  92.  
  93.  
  94.